/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.account.core.domain.account;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import info.peigen.hotpot.business.account.facade.enums.AccountFeeAction;
import info.peigen.hotpot.business.account.facade.enums.AccountTransCode;
import info.peigen.hotpot.business.router.facade.Channel;
import info.peigen.hotpot.common.core.domain.DomainObject;
import info.peigen.hotpot.common.core.lang.money.Money;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;
import lombok.experimental.SuperBuilder;

import java.util.Date;
import java.util.List;

/**
 * <b>(AccountTransfer)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/5/27
 */
@EqualsAndHashCode(callSuper = true)
@SuperBuilder
@Data
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Accessors(chain = true)
public class AccountTransfer extends DomainObject {
    /** 付款账户 */
    Account payerAccount;
    /** 收款账户 */
    Account payeeAccount;
    /** 备付金账户-单边账需要用到 */
    Account provisionsAccount;
    /** 收款渠道编码 */
    Channel receiptChannel;
    /** 提现渠道编码 */
    Channel withdrawChannel;
    /** 付款金额-包含币种 */
    @NotNull
    Money            transAmount;
    /** 账户交易代码 */
    @NotNull
    AccountTransCode transCode;
    /** 交易Id */
    @NotEmpty
    String           tradeId;
    /** 清算时间 */
    @NotNull
    Date             settleTime;
    /** memo */
    String transMemo;

    // 收费相关
    /** 被收费的账户 */
    Account feePayerAccount;
    /** 收费账户 */
    Account feePayeeAccount;
    /** 收费金额 */
    @Builder.Default
    Money feeAmount = Money.zero();
    /** 收费行为 */
    AccountFeeAction feeAction;
    /** 收费因由 */
    String           feeMemo;

    /** 渠道收费（手续费/成本）金额 */
    Money  channelFeeAmount;
    /** 渠道收费（手续费/成本）收费因由 */
    String channelFeeMemo;

    /** 账务记录 */
    @Builder.Default
    List<AccountLog> accountLogs     = Lists.newArrayList();
    @Builder.Default
    AccountTransLog  accountTransLog = new AccountTransLog();

    /** 考虑到账户金额存在变更，账务日志需要记录变更前的金额，所以需要对金额进行预记录（改之前） */
    Money historyProvisionsBalance;
    Money historyPayerBalance;
    Money historyPayeeBalance;
    Money historyFeePayerBalance;
    Money historyFeePayeeBalance;

    public List<Account> getAccounts() {
        List<Account> accounts = Lists.newArrayList(payerAccount, payeeAccount);
        if (ObjectUtil.isNotNull(provisionsAccount)) {
            accounts.add(provisionsAccount);
        }

        if (ObjectUtil.isNotNull(feeAmount) && feeAmount.greaterThan(Money.zero())) {
            if (!StrUtil.equals(feePayerAccount.getAccountId(), payerAccount.getAccountId())) {
                accounts.add(feePayerAccount);
            }
            accounts.add(feePayeeAccount);
        }
        return accounts;
    }
}